package com.meilai.project.mapper.system;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.meilai.project.entity.system.UserRoleRelation;
import com.meilai.project.vo.system.UserSalesVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 张驰
 * @date 2020/9/11 13:12
 */
@Mapper
public interface UserRoleRelationMapper extends BaseMapper<UserRoleRelation> {
    Integer countUserRelationByRoleId(@Param("role_ids") List<Long> role_ids);

    Integer countRelationByRoleCodeAndUserId(@Param("user_id") Long user_id, @Param("role_code") String role_code);

    @Select("SELECT DISTINCT " +
            " c.id AS `id`, " +
            " c.`name` AS `name`, " +
            " IF(c.deleted_at IS NULL, 0, 1) AS `deleted`, " +
            " c.`status` AS `status` " +
            "FROM " +
            " role a " +
            " JOIN user_role_relation b ON a.id = b.role_id " +
            " JOIN `user` c ON b.user_id = c.id  " +
            "WHERE " +
            " a.`code` = 'SalesConsultant';")
    List<UserSalesVO> getSalesman();

    @Select("SELECT DISTINCT " +
            " c.id AS `id`, " +
            " c.`name` AS `name`, " +
            " IF(c.deleted_at IS NULL, 0, 1) AS `deleted`, " +
            " c.`status` AS `status` " +
            "FROM " +
            " role a " +
            " JOIN user_role_relation b ON a.id = b.role_id " +
            " JOIN `user` c ON b.user_id = c.id  " +
            "WHERE " +
            " a.`code` = 'PropertyDevelopment';")
    List<UserSalesVO> getPropertyDevelopment();

    @Select("SELECT count(1) " +
            "FROM `user` u " +
            "JOIN user_role_relation urr ON u.id=urr.user_id " +
            "JOIN role_router_relation rrr ON urr.role_id=rrr.role_id " +
            "JOIN router r ON rrr.router_id=r.id AND r.`name`=#{router_name} " +
            "WHERE u.id=#{user_id} ")
    Integer userHasRouter(@Param("user_id") Long user_id, @Param("router_name") String router_name);
}
